package com.kuyou.ddhm_tv.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kuyou.ddhm_tv.MainActivity;
import com.kuyou.ddhm_tv.R;
import com.kuyou.ddhm_tv.adapter.CategoryAdapter;
import com.kuyou.ddhm_tv.bean.HomeItemBean;
import com.kuyou.ddhm_tv.utils.BsJudge;

import java.util.ArrayList;
import java.util.List;

public class FCategory extends Fragment {
    CategoryAdapter adapter;

    RecyclerView rlCategory;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.f_category,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initHomeItem();
    }

    public void initView(View view){
        rlCategory = view.findViewById(R.id.rl_category);
    }

    public void initHomeItem(){
        RecyclerView.LayoutManager  layoutManager = new GridLayoutManager(getContext(),3);
        rlCategory.setLayoutManager(layoutManager);
        adapter = new CategoryAdapter(getContext());
        adapter.setList(getHomeList());
        adapter.setOnItemClick(new CategoryAdapter.OnItemClick() {
            @Override
            public void OnClik(HomeItemBean homeItemBean) {
                if(homeItemBean.fragment==null){
                    Toast.makeText(getContext(), "暂未开放", Toast.LENGTH_SHORT).show();
                    return;
                }
                Bundle bundle = new Bundle();

                Log.d("HomeClick","clicked");
                switch (homeItemBean.id){
                    case 5:
                        bundle.putSerializable("from", BsJudge.UA);
                        break;
                    case 6:
                        bundle.putSerializable("from", BsJudge.TB);
                        break;
                    case 7:
                        bundle.putSerializable("from", BsJudge.BK);
                        break;
                    case 8:
                        bundle.putSerializable("from", BsJudge.BS);
                        break;

                }
                homeItemBean.fragment.setArguments(bundle);
                ((MainActivity)getActivity()).translateTestingFragment(homeItemBean.fragment);

            }
        });
        rlCategory.setAdapter(adapter);
    }

    public List<HomeItemBean> getHomeList(){
        List<HomeItemBean> list = new ArrayList<>();

        list.add(new HomeItemBean(1,R.mipmap.bp,"血压测量",new FBP()));
        list.add(new HomeItemBean(2,R.mipmap.bp,"体温测量",null));
        list.add(new HomeItemBean(3,R.mipmap.elc,"心电测量",null));
        list.add(new HomeItemBean(4,R.mipmap.bos,"血氧测量",null));
        list.add(new HomeItemBean(5,R.mipmap.ua,"尿酸测量",null));
        list.add(new HomeItemBean(6,R.mipmap.cho,"总胆测量",null));
        list.add(new HomeItemBean(7,R.mipmap.lk,"血酮测量",null));
        list.add(new HomeItemBean(8,R.mipmap.bs,"血糖测量",null));
        list.add(new HomeItemBean(9,R.mipmap.mer,"体检报告",null));


        return list;
    }
}
